import { createSlice } from "@reduxjs/toolkit";

/* 创建user切片  */
const userSlice = createSlice({
  name: "user",
  initialState: {
    userinfo: {id:null}
  },
  reducers: {
    setUser: (state, action) => {
      const newUserinfo = action.payload;
      if (Object.keys(action.payload).length > 0)
        try {
          new URL(newUserinfo.userAvatar);
        } catch (error) {
          console.log(error);
          newUserinfo.userAvatar =
            import.meta.env.VITE_APP_STATIC_BASEURL + newUserinfo.userAvatar;
        }
      state.userinfo = newUserinfo;
    }
  }
});

export const { setUser } = userSlice.actions; //到出切片
export default userSlice.reducer;
